﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using huaguwang.WebControls;

namespace huaguwang.hgmanage.Power
{
    public partial class ButtonSet_E1 : PageBaseAdmin
    {
        private readonly D_T_Button_Exp _Dal = new D_T_Button_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.EditInit(
               UserConst.Exp_GetUser,
               UserConst.Exp_GetButtonList,
               (i, but) =>
               {
                   switch (i)
                   {
                       case 1:
                           but.CausesValidation = false;
                           break;
                       default: 
                           break;
                   }
               },
              UserConst.Exp_ToolBarErr
            );

            if (!IsPostBack)
            {
                var LD = _Dal.GetMenuList();
                var id = Request.Params["ID"].Exp_IntTryParse();
                this.DropDownList1.DataSource = LD;
                this.DropDownList1.DataTextField = "MenuName";
                this.DropDownList1.DataValueField = "id";
                this.DropDownList1.DataBind();
                ViewState.Add("ID", id);

                if (id > 0)
                {
                    var model = _Dal.GetModel(id);
                    if (model == null || !model.Flag)
                    {
                        //请注意,只要跳转回显示页面在,则必须传入Skey
                        var sKey = this.WebUserToolBar1.EditSKey;
                        Response.Redirect("ButtonSet.aspx?" + sKey);
                    }
                    else
                    {
                        this.DropDownList1.SelectedValue = model.MenuID.Exp_Trim();
                        this.TextBox2.Text = model.ButtonName;
                        this.TextBox3.Text = model.ButtonOrderBy.ToString();
                    }
                }
            }



        }

        /// <summary>
        /// 保存按钮
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            var MenuDal = new D_T_Menu_Exp();
            var model = new M_T_Button();

            model.MenuID = this.DropDownList1.SelectedValue.Exp_IntTryParse();

            var PMode = MenuDal.GetModel(model.MenuID);
            if (PMode == null || PMode.Flag == false)
            {
                this.Exp_PageAlter("菜单资料不存在");
                return;
            }

            model.Flag = true;
            model.id = ViewState["ID"].Exp_IntTryParse();
            model.ButtonName = TextBox2.Text.Exp_Trim();
            model.ButtonOrderBy = TextBox3.Text.Exp_IntTryParse();

            /*
                公共的差错校验
             */


            /*add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-
            //************************************************************************************
            // 修改功能
            //  邹兴武    at    2010－12－2
            //  写日志功能已更改
            //************************************************************************************
            // add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-add-upd-del-*/
            #region 更改
            //var iRef = 0;
            //if (0 >= model.id)
            //{
            //    iRef = _Dal.AddExp(model);
            //}
            //else
            //{
            //    /*
            //        修改的专用判断
            //     */


            //    var Temp = _Dal.ExistsExp(model.id);
            //    if (!Temp)
            //    {
            //        this.Exp_PageAlter("资料不存在,可能被其他人删除");
            //        return;

            //    }

            //    iRef = _Dal.UpdateExp(model);
            //}

            //if (iRef > 0)
            //{
            //    this.Exp_PageWriteLog("", "操作成功:" + iRef, "菜单资料维护", true);
            //    var sKey = Server.UrlEncode(ViewState["SKey"].Exp_Trim());
            //    this.Exp_PageAlterURL("操作成功", "ButtonSet.aspx?SKey=" + sKey);

            //}
            //else
            //{
            //    this.Exp_PageAlter("操作失败");
            //}
            #endregion

            var iRef = 0;
            var le = new Log_Type();
            var id = 0;
            if (0 >= model.id)
            {
                iRef = _Dal.Add(model);
                le = Log_Type.add;
                id = iRef;
            }
            else
            {
                /*
                    修改的专用判断
                 */
                var Temp = _Dal.Exists(model.id);
                if (!Temp)
                {
                    this.Exp_PageAlter("资料不存在,可能被其他人删除");
                    return;

                }
                iRef = _Dal.Update(model);
                le = Log_Type.update;
                id = model.id;
            }

            if (iRef > 0)
            {
                this.Exp_PageWriteLog(le, "按钮信息", id);
                var sKey = this.WebUserToolBar1.EditSKey;
                this.Exp_PageAlterHref("操作成功", "ButtonSet.aspx?" + sKey);

            }
            else
            {
                this.Exp_PageAlter("操作失败");
            }
        }

        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var sKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("ButtonSet.aspx?" + sKey);
        }
    }
}
